package com.mbc.web.common.client.ui.control;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;

public class Pager extends Composite {

	private static PagerUiBinder uiBinder = GWT.create(PagerUiBinder.class);
	@UiField Button btnFirst;
	@UiField Button btnPrevious;
	@UiField Button btnNext;
	@UiField Button btnLast;
	@UiField Label lblStart;
	@UiField Label lblEnd;
	@UiField Label lblTotal;

	public long index = 0;
	public long size = 10;
	public long totalRecord = 0;
	public byte action;
	 
	public final byte first=1;
	public final byte previous=2;
	public final byte next=3;
	public final byte last=4;
	
	interface PagerUiBinder extends UiBinder<Widget, Pager> {
	}

	public Pager() {
		initWidget(uiBinder.createAndBindUi(this));
	}
	
	private void prepareIndex(){
		if(action==first){
			index=0;	
		}
		else if(action==next){
			index+=size;	
		}	
		else if(action==previous){
			index-=size;
		}
		else if(action==last){
			long result=totalRecord/size;
			long mod=totalRecord%size;
			if(mod!=0){
				result+=1;
			}
			index=(int) ((result-1)*size);
		}
	}
	
	public void prepareVisible(){
		if(size == 0)
			this.setVisible(false);
		else
		{
			long result=totalRecord/size;
			long mod=totalRecord%size;
			if(mod!=0){
				result+=1;
			}
			int j=(int) ((result-1)*size); 
			 if(index==0){
				 if(totalRecord<=size){
					 this.setVisible(true);
					 //this.setVisible(getElement(), true);
					btnFirst.setEnabled(false);
					btnPrevious.setEnabled(false);
					btnNext.setEnabled(false);	
					btnLast.setEnabled(false);
					}
				 else{
					btnFirst.setEnabled(false);
					btnPrevious.setEnabled(false);
					btnNext.setEnabled(true);
					btnLast.setEnabled(true);
				 }
				}
			 else if(index==j){
				btnNext.setEnabled(false);
				btnLast.setEnabled(false);
				btnFirst.setEnabled(true);
				btnPrevious.setEnabled(true);
			}
			
			else{
				btnFirst.setEnabled(true);
				btnPrevious.setEnabled(true);
				btnNext.setEnabled(true);
				btnLast.setEnabled(true);
				
			}
			 
			if(totalRecord == 0)
				this.setVisible(false);
		}
		
		if(totalRecord != 0){
			long l_start = index  +  1;
			long l_end = index + size;
			if(l_end > totalRecord)
				l_end = l_end - (l_end - totalRecord);
			lblStart.setText(String.valueOf(l_start));
			lblEnd.setText(String.valueOf(l_end));
			lblTotal.setText(String.valueOf(totalRecord));
		}
	}

	public void btnFirst_ClickHandler(ClickHandler clickHandler)
	{
		final ClickHandler l_clickHandler = clickHandler;
		btnFirst.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				index = 0;
				l_clickHandler.onClick(event);
			}
		});
	}
	
	public void btnPrevious_ClickHandler(ClickHandler clickHandler){
		final ClickHandler l_clickHandler = clickHandler;
		btnPrevious.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				action = Pager.this.previous;
				prepareIndex();
				l_clickHandler.onClick(event);
			}
		});
	}
	
	public void btnNext_ClickHandler(ClickHandler clickHandler){
		final ClickHandler l_clickHandler = clickHandler;
		btnNext.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				action = Pager.this.next;
				prepareIndex();
				l_clickHandler.onClick(event);
			}
		});
	}
	
	public void btnLast_ClickHandler(ClickHandler clickHandler){
		final ClickHandler l_clickHandler = clickHandler;
		btnLast.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				action = Pager.this.last;
				prepareIndex();
				l_clickHandler.onClick(event);
			}
		});
	}
}
